home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / libray / libsurf / fogdeck.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-09  |  2.9 KB  |  130 lines

  1. /*
  2.  * fogdeck.c
  3.  *
  4.  * Copyright (C) 1989, 1991, Craig E. Kolb
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  * fogdeck.c,v 4.1 1994/08/09 08:01:31 explorer Exp
  17.  *
  18.  * fogdeck.c,v
  19.  * Revision 4.1  1994/08/09  08:01:31  explorer
  20.  * Bump version to 4.1
  21.  *
  22.  * Revision 1.1.1.1  1994/08/08  04:52:13  explorer
  23.  * Initial import.  This is a prerelease of 4.0.6enh3, or 4.1 possibly.
  24.  *
  25.  * Revision 4.0  91/07/17  14:40:28  kolb
  26.  * Initial version.
  27.  * 
  28.  */
  29. #include "atmosphere.h"
  30. #include "fogdeck.h"
  31. #ifndef HUGE
  32. #define HUGE HUGE_VAL
  33. #endif
  34.  
  35. Fogdeck *
  36. FogdeckCreate(alt, offset, scale, chaoscale, octaves, color, trans)
  37. Float alt, offset, chaoscale;
  38. Vector *scale;
  39. int octaves;
  40. Color *color, *trans;
  41. {
  42.     Fogdeck *fogdeck;
  43.     static void ComputeFogdeck();
  44.  
  45.     fogdeck = (Fogdeck *)Malloc(sizeof(Fogdeck));
  46.  
  47.     fogdeck->alt = alt;
  48.     fogdeck->octaves = octaves;
  49.     fogdeck->scale = *scale;
  50.     fogdeck->chaoscale = chaoscale;
  51.     fogdeck->offset = offset;
  52.  
  53.     if (color == (Color *)NULL)
  54.         fogdeck->color.r = fogdeck->color.g = fogdeck->color.b = 0.;
  55.     else
  56.         fogdeck->color = *color;
  57.     if (trans == (Color *)NULL)
  58.         fogdeck->trans.r = fogdeck->trans.g = fogdeck->trans.b =
  59.             FAR_AWAY;
  60.     else {
  61.         fogdeck->trans = *trans;
  62.     }
  63.     return fogdeck;
  64. }
  65.  
  66. /*
  67.  * Add fogdeck to the given color.
  68.  */
  69. void
  70. FogdeckApply(fogdeck, ray, pos, dist, color)
  71. Fogdeck *fogdeck;
  72. Ray *ray;
  73. Vector *pos;
  74. Float dist;
  75. Color *color;
  76. {
  77.     Float atten, hitdist, density;
  78.     Color trans;
  79.     Vector endp;
  80.     extern Float ExpAtten(), PAChaos();
  81.  
  82.     /*
  83.      * Find distance from origin at which ray strikes
  84.      * z = fogdeck->alt plane
  85.      */
  86.     if (fabs(ray->dir.z) < EPSILON)
  87.         return;
  88.     hitdist = (fogdeck->alt - ray->pos.z) / ray->dir.z;
  89.     if (hitdist < EPSILON || hitdist > dist)
  90.         return;
  91.     /*
  92.      * Compute ray endpoint
  93.      */
  94.     VecAddScaled(ray->pos, hitdist, ray->dir, &endp);
  95.  
  96.     /*
  97.      * Modify transmissivity based on point of
  98.      * intersection.
  99.      */
  100.     endp.x *= fogdeck->scale.x;
  101.     endp.y *= fogdeck->scale.y;
  102.     endp.z *= fogdeck->scale.z;
  103.  
  104.     density = fogdeck->offset +
  105.             fogdeck->chaoscale * PAChaos(&endp, fogdeck->octaves);
  106.     if (density < EPSILON)
  107.         density = HUGE;
  108.     else
  109.         density = 1. / density;
  110.  
  111.     trans = fogdeck->trans;
  112.     ColorScale(density, trans, &trans);
  113.  
  114.     dist -= hitdist;
  115.     
  116.     atten = ExpAtten(dist, trans.r);
  117.  
  118.     if (trans.r == trans.g &&
  119.         trans.r == trans.b) {
  120.         ColorBlend(color, &fogdeck->color, atten, 1. - atten);
  121.         return;
  122.     }
  123.     color->r = atten*color->r + (1. - atten) * fogdeck->color.r;
  124.  
  125.     atten = ExpAtten(dist, trans.g);
  126.     color->g = atten*color->g + (1. - atten) * fogdeck->color.g;
  127.     atten = ExpAtten(dist, trans.b);
  128.     color->b = atten*color->b + (1. - atten) * fogdeck->color.b;
  129. }
  130.